package com.momoauth.api_client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.momoauth.api_client.entity.MoMo_Dist_Item;
import com.momoauth.api_client.mapper.DistItemMapper;
import com.momoauth.api_client.utils.SecurityUtils;
import com.momoauth.common.core.result.ResponseResurt;
import com.momoauth.common.security.annotation.RequiresRoles;
import com.momoauth.common.security.enumeration.Logical;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/momo_dist")
@Api(tags = "字典项")
@RequiresRoles(value = {"超级管理员","普通管理员"}, logical = Logical.OR)
public class DistItemController {

    @Resource
    DistItemMapper distItemMapper;

    @Resource
    SecurityUtils securityUtils;

    /**
     * 字典项列表
     * @param dist_item
     * @return
     */
    @RequestMapping(value = "/getDistItemList", method = RequestMethod.GET)
    public ResponseResurt getDistItemList(MoMo_Dist_Item dist_item) {
        QueryWrapper<MoMo_Dist_Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id", dist_item.getType_id());
        queryWrapper.eq("type_code", dist_item.getType_code());
        queryWrapper.orderByDesc("id"); //按主键倒序排序
        Page<MoMo_Dist_Item> page = new Page<>(dist_item.getCurrentPage(), dist_item.getPageSize());
        page.setRecords(distItemMapper.getDistItemList(page, queryWrapper));
        return ResponseResurt.success().data(page);
    }

    /**
     * 创建字典项
     * @param dist_item
     * @param bindingResult
     * @param request
     * @return
     */
    @RequestMapping(value = "/createDistItem", method = RequestMethod.POST)
    public ResponseResurt createDistItem(
            @Validated MoMo_Dist_Item dist_item,
            BindingResult bindingResult,
            HttpServletRequest request
    ) {
        // 非法参数验证
        if (bindingResult.hasErrors()) {
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseResurt.error().msg(errorMsg);
        }
        // 判断是否存在重复的标签名
        Integer isExist = distItemMapper.existDistItemKey(dist_item.getItemKey());
        if (isExist != null) {
            return ResponseResurt.error().msg("很抱歉，已存在相同标签名。");
        }
        String token = request.getHeader("Authorization");
        Long userId = Long.parseLong(securityUtils.getClaimName(token,"userId"));
        dist_item.setCreate_userId(userId); //用户ID
        // 执行新增操作
        int isInsert = distItemMapper.insert(dist_item);
        if (isInsert > 0) {
            return ResponseResurt.success().msg("恭喜您，创建成功。");
        }
        return ResponseResurt.error().msg("很抱歉，创建失败！");
    }

    /**
     * 修改字典项
     * @param dist_item
     * @param bindingResult
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateDistItem", method = RequestMethod.GET)
    public ResponseResurt updateDistItem(
            @Validated MoMo_Dist_Item dist_item,
            BindingResult bindingResult,
            HttpServletRequest request
    ) {
        // 非法参数验证
        if (bindingResult.hasErrors()) {
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseResurt.error().msg(errorMsg);
        }
        String token = request.getHeader("Authorization");
        Long userId = Long.parseLong(securityUtils.getClaimName(token,"userId"));
        dist_item.setUpdate_userId(userId); //用户ID
        // 执行更新操作
        int isUpdate = distItemMapper.updateById(dist_item);
        if (isUpdate > 0) {
            return ResponseResurt.success().msg("恭喜您，修改成功。");
        }
        return ResponseResurt.error().msg("很抱歉，修改失败！");
    }

    /**
     * 字典项详情
     * @param dist_item
     * @return
     */
    @RequestMapping(value = "/getDistItemById", method = RequestMethod.GET)
    public ResponseResurt getDistItemById(MoMo_Dist_Item dist_item) {
        dist_item = distItemMapper.selectById(dist_item.getId());
        return ResponseResurt.success().data(dist_item);
    }

    /**
     * 删除字典项
     * @param dist_item
     * @return
     */
    @RequestMapping(value = "/deleteDistItem", method = RequestMethod.GET)
    public ResponseResurt deleteDistItem(MoMo_Dist_Item dist_item) {
        // 执行删除操作（物理删除）
        int isDelete = distItemMapper.deleteById(dist_item.getId());
        if (isDelete > 0) {
            return ResponseResurt.success().msg("恭喜您，删除成功。");
        }
        return ResponseResurt.error().msg("很抱歉，删除失败！");
    }
}
